<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\TagRequest;
use App\Repository\TagRepository;
use Illuminate\Http\Request;

class TagsController extends Controller
{
    private $tagRepository;
    public function __construct(TagRepository $tagRepository)
    {
        $this -> tagRepository = $tagRepository; 
    }

    public function index(TagRequest $request)
    {
        if($request ->ajax()){
            $data = $this -> tagRepository ->getList($request);
            return $this->tableAjax($data);
        }
        return view('admin.tags.index');
    }

    public function create()
    {
        return view('admin.tags.create_and_edit');
    }

    public function store(TagRequest $request)
    {
        $result = $this->tagRepository -> addOrUpdate($request); 
        if($result){
            return $this->success('添加成功');
        }else{
            return $this -> error('添加失败');
        }
    }

    public function destory(TagRequest $request)
    {
        $result = $this->tagRepository -> destory($request);
        if ($result) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }    
    }

    public function edit(TagRequest $request)
    {
        $data = $this->tagRepository->getInfo($request);
        return view('admin.tags.create_and_edit',['info'=>$data]);
    }

    public function update(TagRequest $request)
    {
        $result = $this->tagRepository -> addOrUpdate($request);
        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }

    public function getTags(TagRequest $request)
    {
        $data = $this->tagRepository->getTags($request);
        if ($data) {
            return $this->success('获取成功',$data);
        }else{
            return $this->error('获取失败'); 
        }
    }
}
